# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('libxslt' 'libxslt-devel' 'libxslt-python')
pkgver=1.1.33
pkgrel=2
pkgdesc="XML stylesheet transformation library"
arch=('i686' 'x86_64')
url="http://xmlsoft.org/XSLT/"
license=('custom')
makedepends=('gcc' 'python2' 'libxml2-devel' 'libgcrypt-devel')
checkdepends=('docbook-xml')
options=('!libtool')
source=(http://xmlsoft.org/sources/${pkgname}-${pkgver}.tar.gz
        libxslt-1.1.32-msys2.patch
        libxslt.m4-libxslt-1.1.26.patch
        libxslt-cygwin-reentrant.patch)
sha256sums=('8e36605144409df979cab43d835002f63988f3dc94d5d3537c12796db90e38c8'
            '4ee2a3536bb1e3e28c1fe4b0e4322e593aa85b9abcea4fd9ba1ed1c044645912'
            'c02cd8437064db0921c08e63245c403dad12410d2997715aa7aa36600bfaa895'
            'f57629d5f2ce0ab441dc548fa023aee09780eb2940986a44b4423ba253e190bb')

PYTHON_SITELIB=/usr/lib/python2.7/site-packages
PYTHON_LIB=/usr/lib/python2.7

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  patch -p1 -i ${srcdir}/libxslt.m4-libxslt-1.1.26.patch
  patch -p1 -i ${srcdir}/libxslt-cygwin-reentrant.patch
  patch -p1 -i ${srcdir}/libxslt-1.1.32-msys2.patch

  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  sed -e 's|/usr/bin/python -u|/usr/bin/python2 -u|g' -e 's|/usr/bin/python$|/usr/bin/python2|g' -i python/tests/*.py
  ./configure --build=${CHOST} --host=${CHOST} --target=${CHOST} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-shared \
    --enable-static \
    --with-crypto \
    --with-debugger \
    --with-html-dir=/usr/share/doc \
    --with-html-subdir=libxslt/html \
    --with-python=/usr/bin/python2

  make
  make DESTDIR=${srcdir}/dest install

#  mv ${srcdir}/dest${PYTHON_SITELIB}/${pkgname/lib/msys-}mod.dll ${srcdir}/dest${PYTHON_SITELIB}/${pkgname}mod.dll
  sed -i -e "s:${pkgname/lib/msys-}mod:${PN}mod:g" ${srcdir}/dest${PYTHON_SITELIB}/${pkgname}mod.la

#  mv ${srcdir}/dest/usr/share/doc/${pkgname}-python-${pkgver} ${srcdir}/dest/usr/share/doc/python-${pkgname}
  # rm -f ${srcdir}/dest/usr/lib/python*/site-packages/*.a
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  make check
}

package_libxslt() {
  depends=('libxml2' 'libgcrypt')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/
  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/COPYING "${pkgdir}/usr/share/licenses/${pkgname}/COPYING"
}

package_libxslt-devel() {
  pkgdesc="Libxslt headers and libraries"
  groups=('development')
  options=('staticlibs')
  depends=("libxslt=${pkgver}" 'libxml2-devel' 'libgcrypt-devel')

  mkdir -p ${pkgdir}/usr/{bin,share}
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
  rm -rf ${pkgdir}${PYTHON_SITELIB}
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  rm -rf ${pkgdir}/usr/share/doc/libxslt-python-${pkgver}
}

package_libxslt-python() {
  pkgdesc="Libxslt python modules"
  groups=('python-modules')
  options=('staticlibs')
  install=libxslt-python.install
  depends=("libxslt=${pkgver}" 'python2')

  mkdir -p ${pkgdir}/${PYTHON_SITELIB}
  mkdir -p ${pkgdir}/usr/share/doc
  cp -rf ${srcdir}/dest${PYTHON_SITELIB}/* ${pkgdir}${PYTHON_SITELIB}/
  cp -rf ${srcdir}/dest/usr/share/doc/libxslt-python-${pkgver} ${pkgdir}/usr/share/doc/
  
}
